﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace Game
{
    public static class HelperClass
    {
        public enum Alignment
        {
            TopLeft,
            Top,
            TopRight,
            Right,
            BottomRight,
            Bottom,
            BottomLeft,
            Left,
            Middle
        }
        static public Random Random = new Random();


        public static float RandomBetween(float min, float max)
        {
            return min + (float)Random.NextDouble() * (max - min);
        }

        public static Rectangle AlignedRectangle(float x, float y, float width, float height, Alignment align)
        {
            Rectangle rect = new Rectangle((int)x, (int)y, (int)width, (int)height);
            
            switch(align){
                case Alignment.Bottom:
                    rect.X -= rect.Width / 2;
                    rect.Y -= rect.Height;
                    break;
                case Alignment.BottomLeft:
                    rect.Y -= rect.Height;
                    break;
                case Alignment.BottomRight:
                    rect.X -= rect.Width;
                    rect.Y -= rect.Height;
                    break;
                case Alignment.Left:
                    rect.Y -= rect.Height / 2;
                    break;
                case Alignment.Middle:
                    rect.X -= rect.Width / 2;
                    rect.Y -= rect.Height / 2;
                    break;
                case Alignment.Right:
                    rect.X -= rect.Width;
                    rect.Y -= rect.Height / 2;
                    break;
                case Alignment.Top:
                    rect.X -= rect.Width / 2;
                    break;
                case Alignment.TopLeft:
                    break;
                case Alignment.TopRight:
                    rect.X -= rect.Width;
                    break;
            }

            return rect;
        }

        public static Vector2 AlignedVector(float x, float y, Vector2 size, Alignment align)
        {
            Vector2 vect = new Vector2((int)x, (int)y);

            switch (align)
            {
                case Alignment.Bottom:
                    vect.X -= size.X / 2;
                    vect.Y -= size.Y;
                    break;
                case Alignment.BottomLeft:
                    vect.Y -= size.Y;
                    break;
                case Alignment.BottomRight:
                    vect.X -= size.X;
                    vect.Y -= size.Y;
                    break;
                case Alignment.Left:
                    vect.Y -= size.Y / 2;
                    break;
                case Alignment.Middle:
                    vect.X -= size.X / 2;
                    vect.Y -= size.Y / 2;
                    break;
                case Alignment.Right:
                    vect.X -= size.X;
                    vect.Y -= size.Y / 2;
                    break;
                case Alignment.Top:
                    vect.X -= size.X / 2;
                    break;
                case Alignment.TopLeft:
                    break;
                case Alignment.TopRight:
                    vect.X -= size.X;
                    break;
            }            
            return vect;
        }
        /// <summary>
        /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
        /// </summary>
        /// <param name="characters">Unicode Byte Array to be converted to String</param>
        /// <returns>String converted from Unicode Byte Array</returns>

        private static String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);
        }

        /// <summary>
        /// Converts the String to UTF8 Byte array and is used in De serialization
        /// </summary>
        /// <param name="pXmlString"></param>
        /// <returns></returns>
        private static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }

        public static void SerializeObject(Object pObject, string fileName)
        {
            FileStream fStream = null;
            XmlSerializer xs = null;
            XmlTextWriter xmlTextWriter = null;
            try
            {
                fStream = new FileStream(fileName, FileMode.Create);
                xs = new XmlSerializer(pObject.GetType());
                xmlTextWriter = new XmlTextWriter(fStream, Encoding.Default);

                xs.Serialize(xmlTextWriter, pObject);
                fStream.Close();
            }
            catch (Exception e)
            {
                if (fStream != null)
                {
                    fStream.Close();
                }
                System.Console.WriteLine(e);                
            }
        }

        /// <summary>
        /// Method to reconstruct an Object from XML string
        /// </summary>
        /// <param name="pXmlizedString"></param>
        /// <returns></returns>
        public static object DeserializeObject(Type type, String fileName)
        {
            XmlSerializer xs = null;
            FileStream fStream = null;
            XmlTextWriter xmlTextWriter = null;
            Object obj = null;
            try
            {
                xs = new XmlSerializer(type);
                fStream = new FileStream(fileName, FileMode.Open);
                xmlTextWriter = new XmlTextWriter(fStream, Encoding.Default);
                obj = xs.Deserialize(fStream);
                fStream.Close();
                return obj;
            }
            catch
            {
                if (fStream != null)
                {
                    fStream.Close();
                }
                return null;
            }
        }

    }
}
